package com.vates.gestion.dao.impl;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.vates.gestion.dao.BaseDao;
import com.vates.gestion.entidades.Hotel;

@Repository(value = "hotelDao")
public class HotelDaoImpl implements BaseDao<Hotel> {

	private EntityManager em;

	/*
	 * Sets the entity manager
	 */

	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;

	}

	@Override
	@Transactional(readOnly = false)
	public Hotel nuevo(Hotel entidad) {

		return em.merge(entidad);
	}

	@Override
	public Hotel buscarPorId(Long id) {

		return em.find(Hotel.class, id);

	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public List<Hotel> buscarTodos() {

		return em.createQuery("select h from Hotel h order by h.id")
				.getResultList();

	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public List<Hotel> buscarPorNombre(String nombre) {

		return em
				.createQuery(
						"select h from Hotel h where h.nombre LIKE :nombre order by h.id")
				.setParameter("nombre", nombre).getResultList();
	}

	@Override
	public void borrar(Hotel entidad) {

		em.remove(entidad);

	}

}
